<template>
	<view class="content">
		<status-bar ref='statusbar' title='支付' back='0'></status-bar>
		
		
		<view class="row f-column a-center">
			<text class="black size36 bold">支付积分</text>
			<view class="d-flex a-center black bold m-top20">
				<text class="size40">￥</text>
				<text class="size54 m-left10">{{money}}</text>
			</view>
		</view>
		
		<view class="company">
			<text class="color_99 size26">收款方</text>
			<text class="black size26 bold">中谷亚态（重庆）信息技术咨询服务有限公司</text>
		</view>
		
		<view class="palyBtn" @click="Paly" v-show="!flag">立即支付</view>
	
		<button open-type="launchApp" class="palyBtn" :app-parameter="money" binderror="launchAppError" v-show="flag">返回APP</button>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				money:0,
				v_id:'',
				payID:'',
				bid:'I6bEsa714aSTTwSXHNxR6w==',
				payItem:'',
				flag:false,
			}
		},
		onLoad(opt) {
			if(opt.v_id) this.v_id = opt.v_id;
			if(opt.money) this.money = opt.money;
			if(opt.payID) this.payID = opt.payID;
			this.getOpenid();
		},
		
		methods: {
			
			Paly(){
				let that = this;
				if(!that.payItem) return this.$Toast(``);
				if(that.payItem.extraData){
					wx.openEmbeddedMiniProgram({
						appId:Result.appId,
						extraData:Result.extraData
					 });
				}else{
					uni.requestPayment({
						provider: 'wxpay',
						orderInfo: that.payItem,
						timeStamp: that.payItem.timeStamp,
						nonceStr: that.payItem.nonceStr,
						package: that.payItem.package,
						signType: that.payItem.signType,
						paySign: that.payItem.paySign,
						success: (res) => {
							that.flag = true;
							that.$Toast('支付成功')
						},
						fail: (err) => {
							that.$Toast('支付失败');
						}
					})
				}
				
				
			},
			getOpenid(){
				let that = this;
				that.$showLoading('');
				const info = uni.getAccountInfoSync();
				const appId = info.miniProgram.appId;
				uni.getProvider({
					service: "oauth",
					success(res) {
						uni.login({
							provider: res.provider[0],
							success(req) {
								that.$http.get({
									url:'/zzj_singleSaleApi/getOpenId',
									data:{
										js_code: req.code,
										appid: appId,
										bid:that.bid,
									}
								}).then(res=>{
									if (res.code != 100) return that.$Toast(res.msg);
									const result = res.data;
									if(that.payID){
										that.accountPay(result);
									}else{
										that.getPayInfo(result);
									}
									
								})
							}
						})
					},
					complete() {
						that.$hideLoading();
					}
				})
			},
			
			
			
			accountPay(result){
				let that = this;
				that.$showLoading('');
				that.$http.post({
					url:'https://www.yataiunion.com/accountPay/cashierStorePageAjax',
					data:{
						payID:that.payID,
						v_id:that.v_id,
						money:that.money,
						payType:14,
						openId:result.openid,
					}
				}).then(res=>{
					that.$hideLoading();
					if(res.code != '100') return this.$Toast('获取失败');
					const result = res.data;
					that.payItem = result;
				})
			},
			
			
			getPayInfo(result){
				let that = this;
				that.$showLoading('');
				that.$http.get({
					url:'/BrokerApi/getPayInfo',
					data:{
						bid:that.bid,
						v_id:that.v_id,
						pay_way:'wx_lite',
						open_id:result.openid,
						money:that.money,
					}
				}).then(res=>{
					if(res.code != 200){
						that.$hideLoading();
						that.$Toast(res.msg);
						return; 
					}
					const result = res.data;
					that.payItem = result;
					that.$hideLoading();
				})
			},
			launchAppError(e){
				console.log(e.detail.errMsg)
			},
		}
	}
</script>

<style>
	page{
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	.content{
		width:100%;
		height: 100vh;
		padding-top:50rpx;
	}
	.palyTxt{
		color: #000;
		font-size:42rpx;
		font-weight: bold;
	}
	.palyBtn{
		width:690rpx;
		height: 80rpx;
		margin:50rpx auto;
		background-color: #05c160;
		display: flex;
		justify-content: center;
		align-items: center;
		color:#fff;
		font-size:30rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}
	.company{
		width:100%;
		padding:30rpx;
		margin-top:30rpx;
		background-color: #fff;
		border-top:1px solid #e6e6e6;
		border-bottom: 1px solid #e6e6e6;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
</style>
